#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

#define START   100
#define END     300
#define CNT     4

int is_prime(int num);
static void *thr_job(void *s);

typedef struct range_t {
    int start;
    int end;
} range_t;

int main(void)
{
    pthread_t tid[CNT];
    int err, i, step;
    range_t range[CNT];

    step = (END - START + 1) / CNT;

    for (i = 0; i < CNT; i++) {
        range[i].start = START + step * i;
        range[i].end = ((i == CNT - 1) ? END + 1 : range[i].start + step); // 剩余的全放最后一组

        err = pthread_create(tid + i, NULL, thr_job, &range[i]);
        if (err) {
            fprintf(stderr, "pthread_create():%s", strerror(err));
            return 1;
        }
    }

    for (i = 0; i < CNT; i++)
        pthread_join(tid[i], NULL);

    printf("\n");

    return 0;
}

int is_prime(int num)
{
    int i;
    if (num < 2)
        return 0;

    for (i = 2; i <= num / i; i++) {
        if (num % i == 0)
            return 0;
    }

    return 1;
}

static void *thr_job(void *s)
{
    range_t *range = (range_t *)s;
    int i;

    sleep(1);
    for (i = range->start; i < range->end; i++) {
        if (is_prime(i))
            printf("%d ", i);
    }
}

